package cn.iocoder.an.module.mold.controller.admin.storage;

import cn.iocoder.an.module.mold.controller.admin.drawingno.vo.DrawingNoImportExcelVO;
import cn.iocoder.an.module.mold.controller.admin.warehouse.vo.WarehouseRespVO;
import cn.iocoder.an.module.mold.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.an.module.mold.utils.ImportRespVO;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.storage.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.storage.StorageDO;
import cn.iocoder.an.module.mold.service.storage.StorageService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 模具储位")
@RestController
@RequestMapping("/mold/storage")
@Validated
public class StorageController {

    @Resource
    private StorageService storageService;
    @PostMapping("/create")
    @Operation(summary = "创建模具储位")
    @PreAuthorize("@ss.hasPermission('mold:storage:create')")
    public CommonResult<Long> createStorage(@Valid @RequestBody StorageSaveReqVO createReqVO) {
        return success(storageService.createStorage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新模具储位")
    @PreAuthorize("@ss.hasPermission('mold:storage:update')")
    public CommonResult<Boolean> updateStorage(@Valid @RequestBody StorageSaveReqVO updateReqVO) {
        storageService.updateStorage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除模具储位")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:storage:delete')")
    public CommonResult<Boolean> deleteStorage(@RequestParam("id") Long id) {
        storageService.deleteStorage(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除模具储位")
                @PreAuthorize("@ss.hasPermission('mold:storage:delete')")
    public CommonResult<Boolean> deleteStorageList(@RequestParam("ids") List<Long> ids) {
        storageService.deleteStorageListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得模具储位")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:storage:query')")
    public CommonResult<StorageRespVO> getStorage(@RequestParam("id") Long id) {
        StorageDO storage = storageService.getStorage(id);
        return success(BeanUtils.toBean(storage, StorageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得模具储位分页")
    @PreAuthorize("@ss.hasPermission('mold:storage:query')")
    public CommonResult<PageResult<StorageRespVO>> getStoragePage(@Valid StoragePageReqVO pageReqVO) {
        PageResult<StorageDO> pageResult = storageService.getStoragePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StorageRespVO.class));
    }



    @GetMapping("/export-excel")
    @Operation(summary = "导出模具储位 Excel")
    @PreAuthorize("@ss.hasPermission('mold:storage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStorageExcel(@Valid StoragePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StorageDO> list = storageService.getStoragePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "模具储位.xls", "数据", StorageRespVO.class,
                        BeanUtils.toBean(list, StorageRespVO.class));
    }


    @GetMapping("/list-of-warehouse")
    @Operation(summary = "获得保养类型列表")
    public CommonResult<List<StorageRespVO>> getUpkeepCategoryList(@RequestParam("warehouse_id") Long id) {
        List<StorageDO> list = storageService.listOfWarehouse(id);
        return success(BeanUtils.toBean(list, StorageRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得保养类型列表")
    public CommonResult<List<StorageRespVO>> getUpkeepCategoryALL() {
        List<StorageDO> list = storageService.getStorageAll();
        return success(BeanUtils.toBean(list, StorageRespVO.class));
    }

    @GetMapping("/get-import-template")
    @Operation(summary = "获得模具储位导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<StorageImportExcelVO> list = Arrays.asList(
                StorageImportExcelVO.builder()
                        .name("测试")
                        .code("MH-01")
                        .status(1)
                        .warehouseCode("MH-01")
                        .number(0L)
                        .description("测试")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "模具储位导入模板.xlsx", "列表", StorageImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入图号")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<StorageImportExcelVO> list = ExcelUtils.read(file, StorageImportExcelVO.class);
        return success(storageService.importStorageList(list, updateSupport));
    }

}